/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.vienthonga.hibernate.dao;

import com.vienthonga.hibernate.pojo.TRole;
import com.vienthonga.hibernate.pojo.TUser;
import com.vienthonga.hibernate.utils.HibernateUtil;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author DoanHa
 */
public class UserDAO {

    /**
     * @param args the command line arguments
     */
    public boolean RegisterAccount(String fullName,String email,String password,String roleName)
    {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction trans = session.beginTransaction();
        try
        {
            //System.out.println(new Date().toString());
            DateFormat dateFormat = new SimpleDateFormat("yyyyMMddhhmm");
            //System.out.println(dateFormat.format(new Date()));

            TUser newUser = new TUser();
            newUser.setUFullname(fullName);
            newUser.setUEmail(email);
            newUser.setUPassword(password);
            newUser.setUInsertYmd(dateFormat.format(new Date()));
            newUser.setUStatus(1);

            String hql = "from TRole where RName = :roleName";
            Query query = session.createQuery(hql);
            query.setString("roleName", roleName);
            TRole role = (TRole)query.uniqueResult();
            
            if(role != null)
            {
                newUser.setTRole(role);
                session.save(newUser);
                trans.commit();

                return true;
            }
            
            return false;
            
        }
        catch(HibernateException ex)
        {
            System.out.println(ex.getMessage());
            trans.rollback();
        }
        finally
        {            
            session.close();
        }

        return false;
    }

    public boolean CheckExistEmail(String email)
    {
        Session session = HibernateUtil.getSessionFactory().openSession();
        try
        {
            String hql = "from TUser where UEmail = :email";
            Query query = session.createQuery(hql);
            query.setString("email", email);
            List<TUser> userList = query.list();
            if(userList.size() >= 1)
            {
                return true;
            }
            return false;
        }
        catch(HibernateException ex)
        {
            System.out.println(ex.getMessage());
            return true;
        }
        finally
        {
            session.close();
        }
       
    }   

     public static void main(String[] args) {
        // TODO code application logic here
         UserDAO userDao = new UserDAO();
         boolean kq = userDao.CheckExistEmail("haseo@yahoo.vn");
         System.out.println(kq);
    }
}
